from idc import *
from idaapi import *
from idautils import *

# file name
func1 = 0x45339A
# function name
func2 = 0x4535F2

count1 = 0

for ref in CodeRefsTo(func1, 1):
    #print "    called from %s( 0x%x)" % (GetFunctionName(ref), ref)
    preRef = PrevHead(ref, 7)
    #print "    preRef 0x%x" % preRef
    #print GetMnem(preRef)
    #str1 = Dword(preRef+1)

    lpszFile = GetOperandValue(preRef,0)

    #print GetString(str1, -1, ASCSTR_C)

    func = get_func(preRef)
    if func is None:
        break

    found = 0
    
    for funcea in range(preRef,func.endEA):
        if found == 1:
            break
        strref = get_first_fcref_from(funcea)
        while strref != BADADDR:
           #print "  called from 0x%x(%s)" % (funcea,get_name(BADADDR,ref))
           if strref == func2:
               #print "  called from 0x%x(%s)" % (funcea,get_name(BADADDR,strref))
               found = 1
               strlist = []
               for strEA in range(preRef, funcea):
                   for str1 in  DataRefsFrom(strEA):
                       strlist.append(GetString(str1, -1, ASCSTR_C))
               #print strlist
               if len(strlist) > 1:
                   functypelist = set(['-->%s()\n','-->%s::%s(%08lx)\n','-->%s::~%s(%08lx)\n'])
                   for i, str2 in enumerate(strlist):
                       if str2 in functypelist:
                           #print strlist[i-1], strlist[i]
                           filename2 = strlist[i-2].rpartition('\\')[2].rstrip('.cpp')
                           funcname2 = strlist[i-1]
                           funtail = ''
                           if strlist[i] == '-->%s::~%s(%08lx)\n':
                               funtail = '_D' 
                           count1 = count1 + 1
                           print "count is: %d" % count1, '-'*40
                           print filename2 + '__' + funcname2
                           MakeNameEx(func.startEA, filename2 + '__' + funcname2 + funtail, 1 | SN_AUTO)
                           
               
           strref = get_next_fcref_from(funcea, strref)

    

